package com.hcy.rabbitruntimeapi.center;

import com.hcy.rabbitbasicmodel.common.MyWebSocketMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/12/15 19:14
 */
@Component
public class CenterHandlerLink {

    Logger logger = LoggerFactory.getLogger(CenterHandlerLink.class);

    private final ConcurrentHashMap<Integer, MessageHandler> link = new ConcurrentHashMap<>();

    @Async
    public void handler(MyWebSocketMessage myWebSocketMessage) {
//        logger.info("dispatcher handler start message -> [{}]",ToStringBuilder.reflectionToString(myWebSocketMessage));
        MessageHandler messageHandler = link.get(myWebSocketMessage.getCode());
        try {
            messageHandler.handler(myWebSocketMessage);
        } catch (Exception e) {
            logger.info("消息无法解析！");
        }
    }

    public void addHandler(Integer code, MessageHandler messageHandler) {
        link.put(code, messageHandler);
    }

}
